<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mail extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('mail_model');
		/*
		$this->load->model('contact_model');	
		$this->load->model('company_model');	
		$this->load->model('titles_model');	
		*/ 
		
		$this->load->library('email');
	}
	
	public function setting()
	{
		$data = array();
		$data['settings'] = $this->mail_model->find(1);
		$this->load->view('mail/main', $data);
		$this->log('Mail', 'View settings : '.$this->session->userdata['ip_address']);
	}
	
	//Send a preview mail to the current user
	function preview()
	{
		$object = new stdClass();
		$object->email  = $this->session->userdata['email'];
		$object->title_en  = "Mr.";
		$object->name_en  = $this->session->userdata['name'];
		$object->con_id  = $this->session->userdata['user_id'];
		
		$data[] = $object;
		
        $lang 		= "en";
		$template 	= $this->input->post('temp_id');
		
		$this->load->library('email');
		//$result = $this->mail_model->send_to($data);
		
		$mail = $this->email->send_mass($data, $lang, $template);
		//$xx['msg'] = ($pdf==null ? 'Success' : 'Error');
		$this->log('Mail', 'Send Preview Template (ID: '.$template.') : '.$this->session->userdata['ip_address']);
		if(!$mail){	
			//print_r($mail);
			die('No Data Found');
		}
			
		die(json_encode($mail));	
	}
	
        
	function save_settings()
	{
		$data = array();
		$this->form_validation->set_rules('footer_ar'		, 'Arabic Footer'		, 'trim|required|min_length[3]');
		$this->form_validation->set_rules('footer_en'		, 'English Footer'		, 'trim|required|min_length[3]');
		$this->form_validation->set_rules('header_ar'		, 'Arabic Header'		, 'trim|required|min_length[3]');
		$this->form_validation->set_rules('header_en'		, 'English Header'		, 'trim|required|min_length[3]');
		$this->form_validation->set_rules('sender_name'		, 'Arabic Sender Name'	, 'trim|required|min_length[3]');
		$this->form_validation->set_rules('sender_name_en'	, 'English Sender Name'	, 'trim|required|min_length[3]');
		$this->form_validation->set_rules('system_email'	, 'System Email'		, 'trim|required|min_length[3]|valid_email');
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['error']= true;
			$data['msg']  = $this->form_validation->getErrorsArray();
			
			die(json_encode($data));
		}
		else
		{
			$form['footer'] 		= $this->input->post('footer_ar');
			$form['footer_en'] 		= $this->input->post('footer_en');
			$form['header'] 		= $this->input->post('header_ar');
			$form['header_en'] 		= $this->input->post('header_en');
			$form['sender_name'] 	= $this->input->post('sender_name');
			$form['sender_name_en'] = ucwords(strtolower($this->input->post('sender_name_en')));
			$form['system_email'] 	= $this->input->post('system_email');

			//Edit (update)
				if($this->mail_model->update(1, $form)){
					$data['error']= false;
					$data['msg']= ' Successfully Updated..';
					$this->log('Mail', 'Change Email settings : '.$this->session->userdata['ip_address']);
				}
				else{
					$data['error']= true;
					$data['msg']  = 'Data Base Error <br> ERROR: mail/upd/101';
				}
			die(json_encode($data));
		}
	}
	
	function template_save()
	{
		$this->form_validation->set_rules('subject'		, 'Subject'		, 'trim|required|min_length[3]');
		$this->form_validation->set_rules('content'		, 'Content'		, 'trim|required|min_length[3]');
		
		$form['id']		 		= $this->input->post('id');
		$form['subject'] 		= $this->input->post('subject');
		$form['content'] 		= $this->input->post('content');
		
		
		//Update (Edit)
		if(isset($form['id']) && $form['id']>0){
			$this->db->where('id', $form['id']);
			$result = $this->db->update('mail_template',$form);
			
			$this->log('Mail', 'Updated Template (ID: '.$form['id'].') : '.$this->session->userdata['ip_address']);
		}else{
		//insert (add)
			$result = $this->db->insert('mail_template', $form);
			$id = $this->db->insert_id();
			$this->log('Mail', 'Create Template (ID: '.$id.') : '.$this->session->userdata['ip_address']);
		}
		if(isset($id))
		die(json_encode(array($result,'id'=>$id)));
		
		die(json_encode($result));
	}
	
	function template($id=NULL)
	{
		if(isset($id)){
			$this->log('Mail', 'View Template (ID: '.$id.') : '.$this->session->userdata['ip_address']);
			die( json_encode($this->db->where('id',$id)->get('mail_template')->result()));
			}
			
		$data['templates'] = $this->db->get('mail_template')->result();
		
		$this->load->view('mail/template', $data);
	}
	
	function delete_template($id=NULL)
	{
		if (isset($id) && !empty($id))
		{	
				if ($this->mail_model->delete_template($id))
				{
					//success
						$data['msg']	= ' Successfully Deleted..';
						$data['error']	= false;
						$this->log('Mail', 'Delete Template (ID: '.$id.') : '.$this->session->userdata['ip_address']);
					}
				else{
					//failed to delete
					$data['error']	= true;
					$data['msg']	= ' Failed to Delete..';
				}
		die(json_encode($data));
		}
	}
	
	
	function upload()
	{
		$this->load->library('ajaxUpload');
		$file = $this->input->get('qqfile');
		// list of valid extensions, ex. array("jpeg", "xml", "bmp")
		$allowedExtensions = array("jpg","jpeg","gif", "png");
		// max file size in bytes
		$sizeLimit = 10 * 1024 * 1024;
		$uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
		$result = $uploader->handleUpload('upload/mail/');
		$this->log('Mail', 'Upload Image (Image: '.$file.') : '.$this->session->userdata['ip_address']);
		die (json_encode($result));
	}
	
	//this function to list all images inside the upload folder
	//currently the view for create javascript array to use with tiny_mce
	function uploaded_images()
	{
		$this->load->helper('directory');
		$data['maps'] = directory_map('./upload/mail', 1);
		
		$this->load->view('mail/image_list.js.php', $data);
	}
	

}
